package sk.stancel.peter.sudoku.activities;

import sk.stancel.peter.sudoku.R;
import sk.stancel.peter.sudoku.Sudoku;
import sk.stancel.peter.sudoku.types.SudokuType;
import sk.stancel.peter.sudoku.types.SudokuTypes;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

	private Button newGame;
	private Button loadGame;
	private Button about;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Typeface font = Sudoku.getFont();

		newGame = (Button) findViewById(R.id.button_newgame);
		newGame.setOnClickListener(this);
		newGame.setTypeface(font);

		loadGame = (Button) findViewById(R.id.button_loadgame);
		loadGame.setOnClickListener(this);
		loadGame.setTypeface(font);

		about = (Button) findViewById(R.id.button_about);
		about.setOnClickListener(this);
		about.setTypeface(font);
	}

	public void onClick(View v) {
		Intent intent;
		final Context context = this;
		switch (v.getId()) {
		case R.id.button_newgame:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.set_sudoku_type);
			builder.setItems(R.array.gametypes, new DialogInterface.OnClickListener() {
			    public void onClick(DialogInterface dialog2, int item) {
			    	Bundle bundle = new Bundle();
			    	boolean bool = true;
			    	SudokuTypes sudokuType = null;
			    	switch (item) {
			    	case 0:
			    		bundle.putBoolean("medium_type", true);
			    		break;
					case 1:
						sudokuType = new SudokuTypes(context);
						bundle.putIntArray("int_array", sudokuType.selectSudokuFromDB(SudokuType.HARD));
						bundle.putBoolean("hard_type", true);
						break;
					case 2:
						sudokuType = new SudokuTypes(context);
						bundle.putIntArray("int_array", sudokuType.selectSudokuFromDB(SudokuType.DIAGONALS));
						break;
					case 3:
						sudokuType = new SudokuTypes(context);
						bundle.putIntArray("int_array", sudokuType.selectSudokuFromDB(SudokuType.POINTS));
						break;
					case 4:
						sudokuType = new SudokuTypes(context);
						bundle.putIntArray("int_array", sudokuType.selectSudokuFromDB(SudokuType.SQUARES));
						break;
					case 5:
						bool = false;
						Dialog dialog = new Dialog(context);
						dialog.show();
						break;
					}
			    	if(bool){
			    		Intent intent = new Intent(getApplicationContext(), SudokuActivity.class);
						intent.putExtras(bundle);
						startActivity(intent);
			    	}
			    }
			});
			AlertDialog alert = builder.create();
			alert.show();
			break;
		case R.id.button_loadgame:
			intent = new Intent(MainActivity.this, SavedSudokuActivity.class);
			startActivity(intent);
			break;
		case R.id.button_about:
			intent = new Intent(MainActivity.this, AboutActivity.class);
			startActivity(intent);
			break;
		}
	}
	
	@Override
	public void onDestroy() {
	    super.onDestroy();
	}
}